package cn.itsource.dd.controller;

import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.util.AxajResult;
import cn.itsource.basic.util.PageInfo;
import cn.itsource.dd.domain.Detail;
import cn.itsource.dd.domain.Type;
import cn.itsource.dd.query.TypeQuery;
import cn.itsource.dd.service.IDetailService;
import cn.itsource.dd.service.ITypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DetailController {
    @Autowired
    private IDetailService detailService;
    @Autowired
    private ITypeService typeService;
    //查询数据明细分页列表展示
    @PatchMapping
    public PageInfo<Detail> findAll(@RequestBody BaseQuery basequery){
        return detailService.findAllByQuery(basequery);
    }

    //查询所有的数据字典类型
    @PatchMapping("/types")
    public List<Type> findAll(){
        List<Type> list = typeService.findAll();
        return list;
    }
    /**
     * 删除数据字典类型通过id
     */
    @DeleteMapping("/{id}")
    public AxajResult delete(@PathVariable("id")Long id) {
        try {
            detailService.delete(id);
            return new AxajResult();
        } catch (Exception e) {
            return new AxajResult(false,e.getMessage());
        }
    }
    /**
     * 通过id识别是添加还是修改
     */
    @PostMapping
    @ApiOperation(value = "类型添加或修改",notes = "如果有id就是修改否则就是添加")
    public AxajResult save(@RequestBody Detail detail) {
        try {
            detailService.save(detail);
            return new AxajResult();
        } catch (Exception e) {
            return new AxajResult(false,e.getMessage());
        }
    }
    //通过数据字典类型id查询对应的数据明细分页列表展示
    @PatchMapping("/all")
    public PageInfo<Detail> findAllById(@RequestBody TypeQuery typeQuery){
        return detailService.findAllById(typeQuery);
    }

}
